package com.itelite.service.impl;

import com.itelite.domain.dto.UploadUser;
import com.itelite.domain.dto.UserVo;
import com.itelite.domain.pojo.User;
import com.itelite.mapper.UserMapper;
import com.itelite.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 2021/8/6-8:56
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserMapper userMapper;
    @Override
    public int updatePassword(Integer userId,String newPassword) {
        return userMapper.updateUserPassword(userId,passwordEncoder.encode(newPassword));
    }

    @Override
    public int batchCreateUser(List<UploadUser> users) {
        log.info("传入的users:"+users);
        for (UploadUser user : users) {
            user.setStudentId(user.getStudentId().trim());
            user.setUsername("it"+user.getStudentId());
            //导入用户的密码默认为学号
            user.setPassword(passwordEncoder.encode(user.getStudentId()));
            user.setRoles("ROLE_PERSON");
        }
        return userMapper.insertUser(users);
    }

    @Override
    public List<Map<String, String>> queryGroupMemberByKeyword(String keyword) {
        return userMapper.queryUserByKeyword(keyword);
    }

    @Override
    public boolean applyRestPassword(String email) {
        return false;
    }

    @Override
    public List<User> queryUserByEmail(String email) {
        return userMapper.queryUserByEmail(email);
    }

    @Override
    public String queryUserPassword(Integer userId) {
        return userMapper.queryUserPassword(userId);
    }
    @Transactional
    @Override
    public boolean deleteUser(Integer userId) {

        if (userMapper.deleteLeader(userId)>0) {
            log.info("删除的用户为组长");
        }
        return userMapper.deleteUser(userId)>0;
    }

    @Override
    public boolean updateUser(UserVo user) {
        return userMapper.updateUser(user)>0;
    }

    @Override
    public boolean addUser(UploadUser user) {
        user.setUsername("it"+user.getStudentId());
        //导入用户的密码默认为学号
        user.setPassword(passwordEncoder.encode(user.getStudentId()));
        List<UploadUser> users = new ArrayList<>();
        users.add(user);
        return userMapper.insertUser(users)>0;
    }

    @Override
    public boolean validateEmail(String email) {
        return userMapper.countUserByEmail(email)>0;
    }

    @Override
    public UserVo queryUserVo(Integer userId) {
        return userMapper.queryUserVoByUserId(userId);
    }

    @Override
    public void updateUserEmail(Integer userId, String email) {
        userMapper.updateUserEmail(userId,email);
    }
}
